<?php
/**
 * The template used for displaying page content in page.php
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
?>
<div class="leftpage aleft">
	<?php

		if(has_post_thumbnail()) {the_post_thumbnail('page-thumb');
		} else { ?>
				<img src="<?php echo get_template_directory_uri(); ?>/images/page-thumb.jpg" alt="<?php the_title();?>"/>
	<?php } //endif?>
	<div id="search_event">
	
	<link rel='stylesheet' href="<?php echo bloginfo("stylesheet_directory"); ?>/jqueryui/css/redmond/jquery-ui-1.8.24.custom.css" TYPE="text/css">
		<script>
		jQuery(function() {
			jQuery( "#startdate" ).datepicker({autoSize:false});
			jQuery( "#enddate" ).datepicker({autoSize:false});
		});
		</script>
		<img width="210" height="19" border="0" alt="events calendar" src="<?php echo get_template_directory_uri(); ?>/images/events_search_table.gif">
		<div id="innersearch">
			1. Search By Event Name
			<form action="" method="GET" id="searchbyname">
			<script>
			jQuery(function() {
				jQuery('#searchbyname').submit(function() {
					if(jQuery("#eventname").val() == "")
					{
						alert('Incorect search string.');
						return false;
					}
				});
			});
			</script>
				<input type="text" id="eventname" name="eventname" value="<?php if($_REQUEST['eventname']) echo $_REQUEST['eventname'];?>"/>
				<input type="submit" value="submit" />
			</form>
			<form action="" method="GET" id="searchbydate">
			<script>
			jQuery(function() {
				jQuery('#searchbydate').submit(function() {
					if(jQuery("#startdate").val() == "" || jQuery("#enddate").val() == "")
					{
						alert('Please fill Start and End date to search.');
						return false;
					} else
					{
						var begin = new Date(jQuery("#startdate").val());
						var end = new Date(jQuery("#enddate").val());
						if(begin > end)
						{
							alert('The start date cannot be after the end date.');
							return false;
						}
					}
					
				});
			});
			</script>
				2. Search By Date <br />
				Start: <br />
				<input type="text" name="startdate" id="startdate" value="<?php if($_REQUEST['startdate']) echo $_REQUEST['startdate']; ?>"/>
				End: <br />
				<input type="text" name="enddate" id="enddate" value="<?php if($_REQUEST['enddate']) echo $_REQUEST['enddate']; ?>"/>
				<input type="submit" value="submit" />
			</form>
		</div>
	</div>
	<div id="print18months">
		<a href="<?php echo home_url(''); ?>/eventscalendar18months" target="_blank">Print 18-Month Calendar</a>
	</div>
</div><!--/.leftpage-->

<div class="mainpage aleft">

	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		<header class="entry-header">
			<h1 class="entry-title"><?php the_title(); ?></h1>
		</header><!-- .entry-header -->
	
		<div class="entry-content">
			<?php
				$search = 0;
				$args = array(
							'post_type'=> 'vs_event',
							'order'    => 'ASC',
							'orderby' => 'begindate',
							'posts_per_page' => -1
						);
				if($_REQUEST['enddate'] && $_REQUEST['startdate'])
				{
					$beginweek = strtotime($_REQUEST['startdate']);
					$endweek = strtotime($_REQUEST['enddate']);
					$_REQUEST['scope'] = 'all';
					$search = 2;
				}
				if($searchstr = $_REQUEST['eventname'])
				{
					$args["post_vs_event_title_like"] = $searchstr;
					$search = 1;
					$_REQUEST['scope'] = 'all';
				}
			?>
			<?php if ( get_post_meta($post->ID, 'content', true) ){ ?>
			<?php 
					$query = new WP_Query( array( 'name' => get_post_meta($post->ID, 'content', true)));
					while ( $query->have_posts() ) : $query->the_post();
						the_content();
					endwhile;

					// Reset Post Data
					wp_reset_postdata();
			
			?>
			<?php } else {?>
			<?php 
				if($search == 2) {
					echo "<p>Your search for events on or through <b>".date("m/d/Y", $beginweek)."</b> to <b>".date("m/d/Y", $endweek)."</b> produced the following: </p>";
				} elseif($search == 1) {
					echo "<p>Your search for <b>\"".$searchstr."\"</b> produced the following: </p>";
				} else the_content(); ?>
			<?php } //endif?>
			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
			<?php

				if($search == 0)
				{
					$weekadd = get_query_var('week');
					if(!get_query_var('week')){
						$_REQUEST['scope'] = 'week';
					}else{
						$_REQUEST['scope'] = get_query_var('week').'-weeks';
					}
									
					$currentweek = strtotime("0:0:0",time()-date('w')*24*3600);
					$weeks_to_add = get_query_var('week')?get_query_var('week'):0;
					$beginweek = strtotime("+$weeks_to_add week", $currentweek);
					$endweek = strtotime("+7 day", $beginweek);
				}
				
				wp_reset_query();// Reset Query
				query_posts( $args );
				// the Loop
				?>
				<br/>
				<div id="events">
				<h2 class="hd-tb"> 
				<?php if($search == 1): ?>
					Search Results
				<?php else: ?>
					Events schedule from <?php echo date('m/d/Y', $beginweek); ?> to <?php echo date('m/d/Y', $endweek); ?>
				<?php endif; ?>
				</h2>
				<table>
					<tbody><tr>
							<td align="left" width="70%"><b>Event</b></td>
							<td align="left" width="30%"><b>Date</b></td>
						</tr>
					</tbody>
				</table>
				<div class="bd-tb">
					<?php if ( have_posts() ) {?>
						<?php while (have_posts()) : the_post();?>
							<table class="eventdetals" style="width: 100%">
								<tr>
									<td style="width: 70%" class="event-title"><?php the_title(); ?></td>
									<td style="width: 30%"><?php echo date('m/d/Y', get_post_meta(get_the_ID(), 'begindate', true)).' - '.date('m/d/Y', get_post_meta(get_the_ID(), 'enddate', true)); ?></td>
								</tr>
								<tr>
									<td colspan="2">
									<b>Type: </b><?php echo get_post_meta(get_the_ID(), 'type', true);?>
									</td>
								</tr>
								<tr>
									<td colspan="2">
									<b>Attendance: </b><?php echo get_post_meta(get_the_ID(), 'attendance', true);?>
									</td>
								</tr>
								<tr>
									<td colspan="2">
									<a class="rmore" href="<?php the_permalink()?>">Event Details</a>
									</td>
								</tr>
							</table>
						<?php global $wp_query; if($wp_query->current_post+1<$wp_query->post_count): ?>
							<div class="line-tb"></div>
						<?php 
							endif; 
							endwhile;
							wp_reset_query();// Reset Query
						} //endif
						else { ?>
							<table class="eventdetals" style="width: 100%">
								<tr><td>No events scheduled on this week.</td></tr>
							</table>
						<?php } ?>
				</div>
				</div> <!-- events -->
				<?php if($search == 0): ?>
					<table width="100%" cellspacing="5" style="border: none; background-color: #fff;">
						<tbody><tr>
							<td align="left">
								<?php if($weekadd > 0): ?>
								<img src="<?php echo bloginfo("stylesheet_directory"); ?>/images/arrow_back.gif" alt="Arrow">&nbsp;<a href="<?php echo home_url(''); ?>/eventscalendar/week/<?php echo $weekadd - 1; ?>">Previous Week</a>			
								<? endif; ?>
							</td>
							<td align="right">
									<a href="<?php echo home_url(''); ?>/eventscalendar/week/<?php echo $weekadd + 1; ?>">Next Week</a>&nbsp;<img alt="Arrow" src="<?php echo bloginfo("stylesheet_directory"); ?>/images/arrow.gif" />
							</td>
						</tr>
					</tbody>
					</table>
				<?php endif; ?>
		</div><!-- .entry-content -->
		<div style="clear:both"></div>
		<!-- .entry-meta -->
	</article><!-- #post-<?php the_ID(); ?> -->
</div><!--/.mainrpage-->
